package zoo.qboard.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import zoo.qboard.dao.QboardDao;
import zoo.qboard.domain.QboardCommand;
import zoo.qboard.validator.QboardValidator;

@Controller
public class QboardUpdateController {
	private String formViewName = "qboardupdate";
	private QboardDao qboardDao;
	@Autowired
	public void setQboardDao(QboardDao qboardDao) {
		this.qboardDao = qboardDao;
	}
	
	@RequestMapping(value="/main/qboardupdate.do", method=RequestMethod.GET)
	public ModelAndView form(@RequestParam("bq_num") int bq_num){
		QboardCommand qboardCommand = qboardDao.selectQBoard(bq_num);
		return new ModelAndView(formViewName, "qboardCommand", qboardCommand);
	}
	@ModelAttribute("qboardCommand")
	public QboardCommand formBacking() {
		return new QboardCommand();
	}
	@RequestMapping(value="/main/qboardupdate.do", method=RequestMethod.POST)
	public String submit(@ModelAttribute("qboardCommand") QboardCommand qboardCommand, BindingResult result){

		if(result.hasErrors()){
			return formViewName;
		}
		System.out.println(qboardCommand);
		qboardDao.updateQboard(qboardCommand);
		return "redirect:/main/qboardlist.do";
	}
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(new QboardValidator());
	}
}
